# Maintainer: Leonidas Spyropoulos <artafinde@archlinux.org>

pkgname=kafka
pkgver=3.6.0
pkgrel=1
_scalaver=2.13
pkgdesc="An open-source distributed event streaming platform"
arch=('any')
url="https://kafka.apache.org/"
license=('Apache')
depends=(bash 'java-runtime=17')
makedepends=(gradle 'java-environment=17')
source=(
    https://downloads.apache.org/$pkgname/$pkgver/$pkgname-$pkgver-src.tgz{,.asc}
    $pkgname.service
    $pkgname.sysusers
    $pkgname.tmpfiles
    zookeeper.service
    set-base_dir.patch
)
sha512sums=('fbfcb8872b92e6d706c9b305841caca22ac65f2171689623be213132046132794824e28a044f05076fc04845257fb84073b1935cc000c89ff101d0ee98c232bb'
            'SKIP'
            '5b61517a2236bbfce86cc372f0000fdd78fd8715fa2ee872972401df6b9950427ef952b3b248479a5fedc0f7caab3df9cf08982dac4ba5ed2d486c96e521eed5'
            'ae31dd1282250295401df11047bfca13263f8ab867df08f4030f1ddbd2dd5b407e5a59674e8d77a02cdf8a33916a3486b308b022b4fcda0374dbf42d9a29ef28'
            '6712da5b177131ad235ac857fbd8fb97d2c8821ce5c15c40f82f638eb8b08ee693d73030bce893a61934b48b20e67d611dffb365979aafe12e448100c0b82981'
            '6aca852f63f62c8df32721a86cf2ce56630ab1bf428111655908113fe337714fe1149865567e7f0ba1da96fd026cccdb851bcb10ac1823e033edc18adb2ba4c6'
            '3739ec988aada505a0eccee6ae27a9ca620547b3117a034d9eec7cae5bc2af26f41aa8bd066945946c19c05e8529034b7a1b1f49ea03a6574760bb6c1a64a7ad')
b2sums=('cd3a760954ae64cbc2f1a5c944affbd7ff922350144a85ee4b2d570e0b90c3d75ccae69cda62649faccebba56316b944b435088ee971c58512143849ea6acdbe'
        'SKIP'
        '0276cf21cf93d26022c484f085da18d4e314d6f2c6270591a3853cb84a63bbe9ad480949479ac0ef69a96b38508bb2fa528a205e666ce9305713d9da8f074711'
        'da8989434085e937bf24735f8ab9cec969293e2db5f0e715e33f9ba91f3cbde864467fd6d9a4ebb37a367cc3c72b364d74c4093dd027fbe3f96c2bd94987d0ac'
        'a19e80d0177eb35f74403d7ea68a228650c5aeaf54211fd0fba9a948aa49662c7b1331a0572dce7f5bb69163ee9d2b84a3823b94e176f108fd42904e56ade9d6'
        '61f3b3d2c34579a8d1d0ac372b08f74940e57de9a5666a53dfb6a5ddb705d4f96b40c5ab8df9cf6ed85bacc405c639d5a8cbde727e642b75ea05da90e58126c1'
        'ced95f54e1efc1cf1e94e933470f0ed19bbb787f0602fbbd7edd188943eda98c31b47debdde2a4cd0c79d32c448dbc6f634162f9ac1f977ba18b67b4376f4901')
# list of trusted signing keys: https://downloads.apache.org/kafka/KEYS
validpgpkeys=(
    '5AFBFDA44492C1240E5A45AFF19D6CB551424672' # Chris Egerton (CODE SIGNING KEY) <cegerton@apache.org>
    '6FB0F28835C48C7C56D2F1F6D3360B139CE19FE8' # David Arthur (CODE SIGNING KEY) <davidarthur@apache.org>
    '42EFF58EC9BDFD20FA7DF8B16CCECEFAC04AC304' # Luke Chen (CODE SIGNING KEY) <showuon@apache.org>
    'D3DF932A03037580D5453F498060FC0DA962AFE5' # Mickael Maison (CODE SIGNING KEY) <mimaison@apache.org>
    '9C1C80A9F910DFCDF11F1E2676AAB84D81427E11' # Divij Vaidya (CODE SIGNING KEY) <divijv@apache.org>
    '0BA3A5C83CF58E807E4750EEF65DC3423D4CD7B9' # Satish Duggana (code signing key) <satishd@apache.org>
)

prepare() {
    cd "$pkgname-$pkgver-src"
    patch -Np1 < "${srcdir}/set-base_dir.patch"
}

build() {
    cd "$pkgname-$pkgver-src"
    /usr/bin/gradle \
        -PscalaVersion=$_scalaver \
        compileJava \
        compileScala \
        compileTestJava \
        compileTestScala \
        releaseTarGz 

    install -vdm 755 "$srcdir/$pkgname-$pkgver-src/dist"
    tar -xf "core/build/distributions/${pkgname}_${_scalaver}-$pkgver.tgz" \
        --strip 1 \
        -C "$srcdir/$pkgname-$pkgver-src/dist"
}

check() {
    cd "$pkgname-$pkgver-src"
    /usr/bin/gradle \
        -PscalaVersion=$_scalaver \
        --continue \
        -PkeepAliveMode=session \
        -PtestLoggingEvents=started,passed,skipped,failed \
        -PignoreFailures=true \
        -PmaxParallelForks=2 \
        -PmaxTestRetries=1 \
        -PmaxTestRetryFailures=10 \
        unitTest
}

package() {
    local _sh_script
    cd "$pkgname-$pkgver-src/dist"

    # remove unneeded and OS specific scripts
    rm -rvf bin/windows

    # make target scripts executable
    chmod 755 bin/*.sh

    # copy application
    install -vdm 755 "$pkgdir/usr/share/$pkgname/"
    cp -dr --no-preserve='ownership' {bin,config,libs,licenses,site-docs} "$pkgdir/usr/share/$pkgname/"

    # symlink config to /etc
    install -vdm 755 "$pkgdir/etc/$pkgname/"
    for _config in config/*; do
    ln -sv /usr/share/$pkgname/${_config} "$pkgdir/etc/$pkgname/${_config#config/}"
    done

    # symlink scripts into PATH
    install -vdm 755 "$pkgdir/usr/bin/"

    for _sh_script in bin/*.sh; do
        ln -sv /usr/share/$pkgname/${_sh_script} "$pkgdir/usr/${_sh_script}"
    done

    # logs directory
    install -vdm 755 "$pkgdir/var/log/$pkgname/"
    # docs
    install -vDm 644 {LICENSE,NOTICE} -t "$pkgdir/usr/share/doc/$pkgname/"
    install -vDm 644 $srcdir/$pkgname.service -t "$pkgdir/usr/lib/systemd/system/"
    install -vDm 644 $srcdir/zookeeper.service -t "$pkgdir/usr/lib/systemd/system/"
    install -vDm 644 $srcdir/$pkgname.sysusers "$pkgdir/usr/lib/sysusers.d/$pkgname.conf"
    install -vDm 644 $srcdir/$pkgname.tmpfiles "$pkgdir/usr/lib/tmpfiles.d/$pkgname.conf"
}
